<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Code by TinhNQ
 * 2014
 **/
class General extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('general');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('general/general_model');
    }

    function form_general() {
        $this->form->set_form_attributes(array(
                'id' => 'general_form',
               'action' => admin_url('*/general/form_fiel_general' , array('id' => $general_id)),
        ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('main_table', 'select',
                array('label' => 'Table chính',
                        'name' => 'main_table',
                        'values'=>$this->general_model->getListTable(),
                        'required' => true,));
        $this->form->add_filed('sencond', 'checkbox',
                array('label' => 'Table phụ',
                        'name' => 'sencond[]',
                        'values'=>$this->general_model->getListTable()));
        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }
    
    function form_fiel_general() {
        $main_table = $this->input->get_param('main_table');
        $result_main_table = $this->general_model->getListColumnNameByTable($main_table);
        $option_display_type = array(
                '0' => 'Ẩn',
                'id' => 'ID',
                'text' => 'TextBox',
                'select' => "Selectbox",
                'checkbox' => "CheckBox",
                'date' => "Datetime",
                'textarea' => "Textarea",
                'editor' => "CKeditor",
                'publish' => "Có/không",
                'alias' => 'alias');

        $this->load->view('admin/general/general', array(
                'data' => $result_main_table,
                'display_field_type' => $option_display_type,
                'main_table' => $main_table));
    }

    function fiel_columns() {
        $this->load->helper('file');
        $main_table = $this->input->get_param('main_table');
        $module_name = $this->input->get_param('module_name');
        $path = APPPATH.'modules/'. $module_name;
        $path_models = $path . '/models';
        if(!is_dir($path_models)) //create the folder if it's not already exists
        {
            mkdir($path_models,0755,TRUE);
        }
        //controller
        $string = read_file(APPPATH.'modules/general/txt/controller.txt');
        $string = str_replace('paper', $module_name, $string);
        $string = str_replace('Paper', ucfirst($module_name), $string);
        $string = str_replace('<<====grid====>>', $this->_getGrid($module_name), $string);
        $string = str_replace('<<====form====>>', $this->_getForm(), $string);
        write_file('application/modules/admin/controllers/'. $module_name . '.php', $string);
        //model
        $string = read_file(APPPATH.'modules/general/txt/model.txt');
        $string = str_replace('opening', $module_name, $string);
        $string = str_replace('Opening', ucfirst($module_name), $string);
        $string = str_replace('OPENNING', ucwords($module_name), $string);
        $string = str_replace('<<--table-->>', $main_table, $string);
        $string = str_replace('<<--filter-->>', $this->_getModel(), $string);
        write_file($path_models .'/'. $module_name . '_model.php', $string);
        
        //model
        $string = read_file(APPPATH.'modules/general/txt/module.txt');
        $string = str_replace('paper', $module_name, $string);
        $string = str_replace('Paper', ucfirst($module_name), $string);
        write_file($path . '/'. $module_name . '.php', $string);

        $this->load->view('admin/general/general_complete', array('code_controller' => $string, 'module_name' => $module_name));
    }

    
    private function _getGrid($module_name) {
        $string_array = '';
        $array_field = $this->input->get_param('field');
        $array_display_name = $this->input->get_param('display_name');
        $array_display_field_type = $this->input->get_param('display_field_type');
        $array_field_type = $this->input->get_param('type');
        $array_showlist = $this->input->get_param('showlist');
        $array_search = $this->input->get_param('search');
        $array_sort = $this->input->get_param('sort');
        $space = "                ";
        foreach ($array_field as $key => $value) {
            if (isset($array_showlist) && array_search($value, $array_showlist) !== false) {
                $string_array .= $space . "array(\r\n";
                $string_array .= $space . "    'name' => '$array_display_name[$key]',\r\n";
                if ($array_display_field_type[$key] != 0) {
                    $string_array .= $space . "    'type' => '$array_display_field_type[$key]',\r\n";
                } else {
                    $type = $this->setValueSelect($array_field_type[$key]);
                    $string_array .= $space . "    'type' => '$type',\r\n";
                }
                $string_array .= $space . "    'index' => '$value',\r\n";
                
                if (isset($array_search) && array_search($value, $array_search) !== false) {
                    $string_array .= $space . "    'filter' => '$value',\r\n";
                }
                if (isset($array_sort) && array_search($value, $array_sort) !== false) {
                    $string_array .= $space . "    'sort_column' => '$value',\r\n";
                }
                if ($array_display_field_type[$key] == 'publish') {
                    $string_array .= $space . "    'options' => array('0' => 'Không', '1' => 'Có'),\r\n";
                }elseif ($array_display_field_type[$key] == 'select') {
                    $string_array .= $space . "    //'options' => 'options' => \$this->".$module_name."_model->get_".$value."_options(),\r\n";
                }

                $string_array .= $space . "),\r\n";
            }
        }
        return $string_array;
    }

    private function _getForm() {
        $string_array = '';
        $array_field = $this->input->get_param('field');
        $array_display_name = $this->input->get_param('display_name');
        $array_display_field_type = $this->input->get_param('display_field_type');
        $array_showlist = $this->input->get_param('showlist');
        $array_requite = $this->input->get_param('requite');
        $array_sort = $this->input->get_param('sort');
        $space = "        ";
        $str_add = $space . "\$this->form->add_filed(";
        foreach ($array_field as $key => $value) {
            if ($array_display_field_type[$key] != "0") {
                if ($array_display_field_type[$key] != 'id') {
                    $string_array .= $str_add . "'$value', ";
                    $string_array .= "'$array_display_field_type[$key]', ";
                    $string_array .= "array('label' => '$array_display_name[$key]', 'name' => '$value', ";
                    if (isset($array_requite) && array_search($value, $array_requite) !== false) {
                        $string_array .= "'required' => true,";
                    }
                    $string_array .= "));\r\n";
                }
            }
        }
        return $string_array;
    }

    private function _getModel() {
        $string_array = '';
        $array_field = $this->input->get_param('field');
        $array_display_name = $this->input->get_param('display_name');
        $array_display_field_type = $this->input->get_param('display_field_type');
        $array_showlist = $this->input->get_param('showlist');
        $array_requite = $this->input->get_param('requite');
        $array_sort = $this->input->get_param('sort');
        $space = "        ";
        foreach ($array_field as $key => $value) {
            if ($array_display_field_type[$key] == "id") {
                $string_array .= $space . "if (isset(\$filters['". $value ."'])) {\r\n";
                $string_array .= $space . "    \$this->db->where('". $value ."', \$filters['". $value ."']);\r\n";
                $string_array .= $space . "}\r\n";
                $string_array .= $space . "if (isset(\$filters['". $value ."_other'])) {\r\n";
                $string_array .= $space . "    \$this->db->where('". $value ." !=', \$filters['". $value ."_other']);\r\n";
                $string_array .= $space . "}\r\n";
                $string_array .= $space . "\$this->db->order_by('". $value ."','DESC');\r\n";
            } elseif ($array_display_field_type[$key] == "date") {
                $string_array .= $space . "if (isset(\$filters['". $value ."_to'])) {\r\n";
                $string_array .= $space . "    \$this->db->where('". $value ." <=', \$filters['". $value ."_to']);\r\n";
                $string_array .= $space . "}\r\n";
                $string_array .= $space . "if (isset(\$filters['". $value ."_from'])) {\r\n";
                $string_array .= $space . "    \$this->db->where('". $value ." =>', \$filters['". $value ."_from']);\r\n";
                $string_array .= $space . "}\r\n";
            } else {
                $string_array .= $space . "if (isset(\$filters['". $value ."'])) {\r\n";
                $string_array .= $space . "    \$this->db->where('". $value ."', \$filters['". $value ."']);\r\n";
                $string_array .= $space . "}\r\n";
            }
        }
        return $string_array;
    }
    private function setValueSelect($string) {
        if (strstr($string, 'varchar') != FALSE) {
            return 'text';
        }
        if (strstr($string, 'tinyint') || strstr($string, 'smallint')) {
            return 'publish';
        }
        if (strstr($string, 'int')) {
            return 'select';
        }
        if (strstr($string, 'datetime') > 0 || strstr($string, 'date')) {
            return 'date';
        }
        if (strstr($string, 'text')) {
            return 'editor';
        }
        return '';
    }
}
?>